$(function() {
    $.ajaxSetup({
        type: "post",
        dataType: "json"
    });

    var sexMap = {
        0: "男",
        1: "女"
    };

    var sexotMap = {
        0: "男",
        1: "女",
        2: "双性恋",
        3: "不确定"
    };

    var loveMap = {
        0: "单身",
        1: "非单身",
        2: "保密"
    };

    //horo_options = ['白羊座', '金牛座', '双子座', '巨蟹座', '狮子座', '处女座', '天秤座',
    //                        '天蝎座', '射手座', '摩羯座', '水瓶座', '双鱼座', '']

    var newFakeUserTemplate = _.template($("#new-fake-template").html());

    $(".add-fake-user").popover({
        html: true,
        placement: "bottom",
        content: newFakeUserTemplate
    });

    $('.add-fake-user').on('hidden.bs.popover', function () {
        $('#fileupload').fileupload({
            url: url,
            dataType: 'json',
            autoUpload: false,
            acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
            maxFileSize: 999000,
            // Enable image resizing, except for Android and Opera,
            // which actually support image resizing, but fail to
            // send Blob objects via XHR requests:
            disableImageResize: /Android(?!.*Chrome)|Opera/
                .test(window.navigator.userAgent),
            previewMaxWidth: 100,
            previewMaxHeight: 100,
            previewCrop: true,
            singleFileUploads: false,
            maxNumberOfFiles: 1
        }).on('fileuploadadd', function (e, data) {
            data.context = $('<div/>').appendTo('#files');
            $.each(data.files, function (index, file) {
                var node = $('<p/>').append($('<span/>').text(file.name));
                if (!index) {
                    node
                        .append('<br>')
                        .append(uploadButton.clone(true).data(data));
                }
                node.appendTo(data.context);
            });
        }).on('fileuploadprocessalways', function (e, data) {
            var index = data.index,
                file = data.files[index],
                node = $(data.context.children()[index]);
            if (file.preview) {
                node
                    .prepend('<br>')
                    .prepend(file.preview);
            }
            if (file.error) {
                node
                    .append('<br>')
                    .append($('<span class="text-danger"/>').text(file.error));
            }
            if (index + 1 === data.files.length) {
                data.context.find('button')
                    .text('Upload')
                    .prop('disabled', !!data.files.error);
            }
        }).on('fileuploadprogressall', function (e, data) {
            var progress = parseInt(data.loaded / data.total * 100, 10);
            $('#progress .progress-bar').css(
                'width',
                progress + '%'
            );
        }).on('fileuploaddone', function (e, data) {
            $.each(data.result.files, function (index, file) {
                if (file.url) {
                    var link = $('<a>')
                        .attr('target', '_blank')
                        .prop('href', file.url);
                    $(data.context.children()[index])
                        .wrap(link);
                } else if (file.error) {
                    var error = $('<span class="text-danger"/>').text(file.error);
                    $(data.context.children()[index])
                        .append('<br>')
                        .append(error);
                }
            });
        }).on('fileuploadfail', function (e, data) {
            $.each(data.files, function (index) {
                var error = $('<span class="text-danger"/>').text('File upload failed.');
                $(data.context.children()[index])
                    .append('<br>')
                    .append(error);
            });
        }).on('fileuploadsubmit', function (e, data) {
            // The example input, doesn't have to be part of the upload form:
            var input = $('#input');
            data.formData = {example: input.val()};
            if (!data.formData.example) {
              data.context.find('button').prop('disabled', false);
              input.focus();
              return false;
            }
        }).prop('disabled', !$.support.fileInput)
            .parent().addClass($.support.fileInput ? undefined : 'disabled');
    })

    $("body").on("click", ".new-submit", function() {
        var name = $("#new-name").val();
        var sex = $("[name=sex]").val();
        var sexot = $("[name=sexot]").val();
        var love = $("[name=love]").val();
        var horo = $("#new-horo").val();
        var intro = $("#new-intro").val();

        var url = "/cms/fakeuser/add";
        var data = {
            name: name,
            sex: sexMap[sex],
            sexot: sexotMap[sexot],
            love: loveMap[love],
            horo: horo,
            intro: intro
        };

        $.ajax({
            url: url,
            data: data,
            success: function(res) {
                if(res.ret == 0) {

                }
            },
            error: function() {

            },
            complete: function() {

            }
        });
    });
});